package mshop.com.schild_kotlin

import android.content.Intent
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.View
import com.wind.me.xskinloader.SkinManager
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.comm_title_layout.*
import mshop.com.schild_kotlin.activitys.SearchActivity
import mshop.com.schild_kotlin.activitys.SetActivity
import mshop.com.schild_kotlin.base.BaseActivity
import mshop.com.schild_kotlin.fragments.FindFragment
import mshop.com.schild_kotlin.fragments.HomeFragment
import mshop.com.schild_kotlin.fragments.HotFragment
import mshop.com.schild_kotlin.fragments.MeFragment
import mshop.com.schild_kotlin.skin.MySkinUtil

class MainActivity : BaseActivity() , View.OnClickListener{

    var homeFragment: HomeFragment? = null
    var findFragment: FindFragment? = null
    var hotFragment: HotFragment? = null
    var meFragment: MeFragment? = null
    var mExitTime:Long=0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initFragment(savedInstanceState)
    }

    override fun getLayoutResources(): Int {
        return R.layout.activity_main
    }

    override fun initView() {
        MySkinUtil.setWindowStatusBarColor(window, R.color.statusBarColor)
        bar_title_tv?.text = "Home"
        initRadioButton()
    }

    private fun initRadioButton() {
        rb_home.isChecked = true
//        MySkinUtil.setTextViewColor(rb_home,R.color.main_bar_title_txt_sel_color)
//        rb_home.setTextColor(resources.getColor(R.color.black))
        rb_home.setOnClickListener(this)
        rb_find.setOnClickListener(this)
        rb_hot.setOnClickListener(this)
        rb_mine.setOnClickListener(this)
        bar_right_iv.setOnClickListener(this)
        bar_right_iv.setTag(1)
    }

    private fun initFragment(savedInstanceState: Bundle?) {
        if(savedInstanceState!=null){
            val mFragments: List<Fragment> = supportFragmentManager.fragments
            for (item in mFragments){
                if (item is HomeFragment){
                    homeFragment=item
                }else if(item is FindFragment){
                    findFragment=item
                }else if(item is HotFragment){
                    hotFragment=item
                }else if(item is MeFragment){
                    meFragment=item
                }
            }

        }else{
            homeFragment=HomeFragment.getInstall()
            findFragment=FindFragment.getInstall()
            hotFragment= HotFragment.getInstall()
            meFragment= MeFragment.getInstall()
            val fragmentTrans =supportFragmentManager.beginTransaction()
            fragmentTrans.add(R.id.fl_content,homeFragment)
            fragmentTrans.add(R.id.fl_content,findFragment)
            fragmentTrans.add(R.id.fl_content,hotFragment)
            fragmentTrans.add(R.id.fl_content,meFragment)
            fragmentTrans.commit()
        }

        supportFragmentManager.beginTransaction().show(homeFragment)
                .hide(findFragment)
                .hide(hotFragment)
                .hide(meFragment)
                .commit()
    }


    private fun clearState() {
        rg_root.clearCheck()
//        MySkinUtil.setTextViewColor(rb_home,R.color.main_bar_title_txt_nor_color)
//        MySkinUtil.setTextViewColor(rb_mine,R.color.main_bar_title_txt_nor_color)
//        MySkinUtil.setTextViewColor(rb_hot,R.color.main_bar_title_txt_nor_color)
//        MySkinUtil.setTextViewColor(rb_find,R.color.main_bar_title_txt_nor_color)

//        rb_home.setTextColor(resources.getColor(R.color.main_bar_title_txt_nor_color))
//        rb_mine.setTextColor(resources.getColor(R.color.main_bar_title_txt_nor_color))
//        rb_hot.setTextColor(resources.getColor(R.color.main_bar_title_txt_nor_color))
//        rb_find.setTextColor(resources.getColor(R.color.main_bar_title_txt_nor_color))
    }


    override fun onClick(v: View?) {
        clearState()
        when (v?.id) {
            R.id.bar_right_iv -> {
                var searchTag = bar_right_iv.getTag() as Int;
                if(searchTag==1){
                    val intent=Intent(this@MainActivity,SearchActivity::class.java)
                    startActivity(intent)
                }else{
                    val intent = Intent(this@MainActivity, SetActivity::class.java)
                    startActivity(intent)
                }

            }
            R.id.rb_find -> {
                rb_find.isChecked = true
//                rb_find.setTextColor(resources.getColor(R.color.black))
//                MySkinUtil.setTextViewColor(rb_find,R.color.main_bar_title_txt_sel_color)

                supportFragmentManager.beginTransaction().show(findFragment)
                        .hide(homeFragment)
                        .hide(meFragment)
                        .hide(hotFragment)
                        .commit()
                bar_title_tv.text = "Discover"
                bar_title_tv.visibility = View.VISIBLE
                MySkinUtil.setImageDrawable(bar_right_iv, R.drawable.icon_search)
                MySkinUtil.setImageTint(bar_right_iv, R.color.common_bar_img_tint_color)
                bar_right_iv.setTag(1)
            }
            R.id.rb_home -> {
                rb_home.isChecked = true
//                MySkinUtil.setTextViewColor(rb_home,R.color.main_bar_title_txt_sel_color)
//                rb_home.setTextColor(resources.getColor(R.color.main_bar_title_txt_sel_color))
                supportFragmentManager.beginTransaction().show(homeFragment)
                        .hide(findFragment)
                        .hide(meFragment)
                        .hide(hotFragment)
                        .commit()
                bar_title_tv.text = "Home"
                bar_title_tv.visibility = View.VISIBLE
                MySkinUtil.setImageDrawable(bar_right_iv, R.drawable.icon_search)
                MySkinUtil.setImageTint(bar_right_iv, R.color.common_bar_img_tint_color)
                bar_right_iv.setTag(1)
            }
            R.id.rb_hot -> {
                rb_hot.isChecked = true
//                rb_hot.setTextColor(resources.getColor(R.color.black))
//                MySkinUtil.setTextViewColor(rb_hot,R.color.main_bar_title_txt_sel_color)

                supportFragmentManager.beginTransaction().show(hotFragment)
                        .hide(findFragment)
                        .hide(meFragment)
                        .hide(homeFragment)
                        .commit()
                bar_title_tv.text = "Ranking"
                bar_title_tv.visibility = View.VISIBLE
                MySkinUtil.setImageDrawable(bar_right_iv, R.drawable.icon_search)
                MySkinUtil.setImageTint(bar_right_iv, R.color.common_bar_img_tint_color)
                bar_right_iv.setTag(1)
            }
            R.id.rb_mine -> {
                rb_mine.isChecked = true
//                rb_mine.setTextColor(resources.getColor(R.color.black))
//                MySkinUtil.setTextViewColor(rb_mine,R.color.main_bar_title_txt_sel_color)

                supportFragmentManager.beginTransaction().show(meFragment)
                        .hide(findFragment)
                        .hide(homeFragment)
                        .hide(hotFragment)
                        .commit()
                bar_title_tv.visibility = View.INVISIBLE
                MySkinUtil.setImageDrawable(bar_right_iv, R.drawable.icon_setting)
                MySkinUtil.setImageTint(bar_right_iv, R.color.common_bar_img_tint_color)
                bar_right_iv.setTag(2)
            }
        }
    }

    override fun onBackPressed() {
        if(System.currentTimeMillis().minus(mExitTime)<=3000){
            finish()
        }else{
            mExitTime=System.currentTimeMillis();
            toastShow("再按一次退出程序")
        }
    }

    override fun getNeedRefresh(): Boolean {
        return false
    }

    override fun onRefresh() {
    }


}
